import {Action} from 'redux';

import immutableObject from '../common/immutableObject';
import {ActionType} from './ActionType';

export interface DesignNamesState {
  [designId: string]: string;
}

export interface DesignNamesAction extends Action {
  type: ActionType;
  designId?: string;
  designName?: string;
}

function designNamesReducer(state: DesignNamesState = {}, action: DesignNamesAction): DesignNamesState {
  switch (action.type) {

    case ActionType.UPDATE_DESIGN_NAME:
      return _updateDesignName(state, action.designId, action.designName);

    case ActionType.REMOVE_DESIGN:
      return _removeDesign(state, action.designId);

    default:
      return state;
  }
}

function _updateDesignName(state: DesignNamesState, designId: string, designName: string): DesignNamesState {
  if (!designId) {
    return state;
  }

  return immutableObject.set(state, designId, designName);
}

function _removeDesign(state: DesignNamesState, designId: string): DesignNamesState {
  if (!designId) {
    return state;
  }

  return immutableObject.remove(state, designId);
}


export default designNamesReducer;
